package com.hertz.appr.service.timer;

import java.util.List;

import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.log4j.Logger;

import com.hertz.appr.pojo.db.Message;
import com.hertz.appr.service.MentionsService;
import com.hertz.appr.util.Configuration;
import com.hertz.appr.util.PropertiesHelper;

public class Timer4Mentions {

	private MentionsService mentionsServiceQQ;
	private MentionsService mentionsServiceSina;
	private Integer counts;
	private Integer times;
	private String provider;
	
	private Logger log = Logger.getLogger(Timer4Mentions.class);
	private boolean running = false;
	public void getMentions(){
		running = true;
		Long since = 0L;
		PropertiesConfiguration config = PropertiesHelper.getConfiguration();
		since = config.getLong(provider + "_mentions_border");
		log.info("start timer getMentions,since=" + since);
		if(provider.equals(Configuration.QQ_PROVIDER)){
			List<Message> list = mentionsServiceQQ.getMentionsNext(since, counts, times);
			if(list != null && list.size() != 0)
				mentionsServiceQQ.fill2DB(list);
		}else if(provider.equals(Configuration.SINA_PROVIDER)){
			List<Message> list = mentionsServiceSina.getMentionsNext(since, counts, times);
			if(list != null && list.size() != 0)
				mentionsServiceSina.fill2DB(list);
		}
		running = false;
		log.info("end timer getMentions");
	}

	public void destroy(){
		log.info("~~~~~~~destroy mentions timer");
		while(running){
			log.info("mentions timer is running");
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public MentionsService getMentionsServiceQQ() {
		return mentionsServiceQQ;
	}

	public void setMentionsServiceQQ(MentionsService mentionsServiceQQ) {
		this.mentionsServiceQQ = mentionsServiceQQ;
	}

	public MentionsService getMentionsServiceSina() {
		return mentionsServiceSina;
	}

	public void setMentionsServiceSina(MentionsService mentionsServiceSina) {
		this.mentionsServiceSina = mentionsServiceSina;
	}

	public Integer getCounts() {
		return counts;
	}

	public void setCounts(Integer counts) {
		this.counts = counts;
	}

	public Integer getTimes() {
		return times;
	}

	public void setTimes(Integer times) {
		this.times = times;
	}

	public String getProvider() {
		return provider;
	}

	public void setProvider(String provider) {
		this.provider = provider;
	}
	
}
